home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / animutil / fastgfx / fg303b / manuals.arj / USER13.DOC < prev    next >
Encoding:
Text File  |  1993-10-02  |  21.6 KB  |  519 lines

  1. Chapter 13
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Special Effects
  8.  
  9. 250   Fastgraph User's Guide
  10.  
  11.  
  12.  
  13. Overview
  14.  
  15.      This chapter will discuss the Fastgraph routines that help produce
  16. special visual effects.  These include the ability to dissolve the screen
  17. contents in small increments, scroll areas of the screen, and change the
  18. physical origin of the screen.  The accompanying example programs illustrate
  19. how to use these routines to produce some interesting effects.
  20.  
  21.  
  22. Screen Dissolving
  23.  
  24.      Screen dissolving is the process of replacing the entire screen contents
  25. in random small increments instead of all at once.  Fastgraph includes two
  26. routines, fg_fadeout and fg_fadein, for this purpose.  The fg_fadeout routine
  27. incrementally replaces the visual page contents with pixels of the current
  28. color, while fg_fadein incrementally replaces the visual page contents with
  29. the hidden page contents (that is, the page defined in the most recent call
  30. to fg_sethpage).  Both routines accept an integer argument that defines the
  31. delay between each incremental replacement.  A value of zero means to perform
  32. the replacement as quickly as possible, while 1 is slightly slower, 2 is
  33. slower yet, and so forth.  The fg_fadeout and fg_fadein routines have no
  34. effect in text video modes.
  35.  
  36.      Example 13-1 shows how to use the fg_fadeout routine.  The program,
  37. which runs in any graphics video mode, first fills the screen with a
  38. rectangle of color 2.  After waiting for a keystroke, the program
  39. incrementally replaces the screen contents with pixels of color 15 (the
  40. current color index when fg_fadeout is called).  After another keystroke, the
  41. program exits gracefully.
  42.  
  43.                                 Example 13-1.
  44.  
  45.                   #include <fastgraf.h>
  46.                   void main(void);
  47.  
  48.                   void main()
  49.                   {
  50.                      int old_mode;
  51.  
  52.                      old_mode = fg_getmode();
  53.                      fg_setmode(fg_automode());
  54.  
  55.                      fg_setcolor(2);
  56.                      fg_rect(0,fg_getmaxx(),0,fg_getmaxy());
  57.                      fg_waitkey();
  58.  
  59.                      fg_setcolor(15);
  60.                      fg_fadeout(0);
  61.                      fg_waitkey();
  62.  
  63.                      fg_setmode(old_mode);
  64.                      fg_reset();
  65.                   }
  66.  
  67.                                             Chapter 13:  Special Effects   251
  68.  
  69.  
  70.      Example 13-2 shows how to use the fg_fadein routine in any 320 by 200
  71. color graphics video mode.  The program first fills the screen with a
  72. rectangle of color 2 and then fills video page 1 with a rectangle of color 1.
  73. After waiting for a keystroke, the program incrementally transfers the
  74. contents of page 1 to the visual page.  After the call to fg_fadein, both
  75. page 0 (the visual page) and page 1 (the hidden page) will contain rectangles
  76. of color 1 that fill the entire video page.  Finally, the program waits for
  77. another keystroke before returning to DOS.
  78.  
  79.                                 Example 13-2.
  80.  
  81.                 #include <fastgraf.h>
  82.                 #include <stdio.h>
  83.                 #include <stdlib.h>
  84.                 void main(void);
  85.  
  86.                 void main()
  87.                 {
  88.                    int new_mode, old_mode;
  89.  
  90.                    new_mode = fg_bestmode(320,200,2);
  91.                    if (new_mode < 0 || new_mode == 12) {
  92.                       printf("This program requires a 320 ");
  93.                       printf("x 200 color graphics mode.\n");
  94.                       exit(1);
  95.                       }
  96.                    old_mode = fg_getmode();
  97.                    fg_setmode(new_mode);
  98.                    fg_allocate(1);
  99.                    fg_sethpage(1);
  100.  
  101.                    fg_setcolor(2);
  102.                    fg_rect(0,319,0,199);
  103.                    fg_setpage(1);
  104.                    fg_setcolor(1);
  105.                    fg_rect(0,319,0,199);
  106.                    fg_waitkey();
  107.  
  108.                    fg_fadein(0);
  109.                    fg_waitkey();
  110.  
  111.                    fg_freepage(1);
  112.                    fg_setmode(old_mode);
  113.                    fg_reset();
  114.                 }
  115.  
  116.  
  117.      You also can produce some appealing visual effects by replacing the
  118. screen contents in a non-random fashion using the fg_restore or fg_transfer
  119. routines.  For example, you could copy the hidden page contents to the visual
  120. page through a series of concentric rectangular areas, each slightly larger
  121. than the previous, until the entire screen is copied.  Another interesting
  122. effect is to start around the screen perimeter and proceed toward the screen
  123. center, thus producing a "snake-like" effect.  Experimenting with such
  124. techniques may reveal other effects that suit your application.
  125. 252   Fastgraph User's Guide
  126.  
  127.  
  128. Scrolling
  129.  
  130.      Another useful effect is scrolling, and Fastgraph provides a routine
  131. that performs vertical scrolling within a given region of the active video
  132. page.  The fg_scroll routine scrolls a region defined in screen space or
  133. character space.  It can scroll up or down and offers two types of scrolling:
  134. circular and end-off.  In circular scrolling, rows that scroll off one edge
  135. of the defined region appear at its opposite edge.  In end-off scrolling,
  136. such rows are simply wind up above or below the scrolling area.  The
  137. following diagrams illustrate the two types of scrolling.
  138.  
  139.                 end-off scrolling            circular scrolling
  140.  
  141.               before         after          before         after
  142.  
  143.                                C                             B
  144.                  A                             A
  145.  
  146.                                A                             A
  147.                  B                             B
  148.  
  149.  
  150.      In these diagrams, the area bounded by the double lines is the scrolling
  151. region, as specified in the call to fg_scroll.  Also, the scrolling direction
  152. is assumed to be down (that is, toward the bottom of the screen), and the
  153. number of rows to scroll is the height of the area designated B.  The number
  154. of rows to scroll is often called the scrolling increment.
  155.  
  156.      For the end-off scrolling example, the scrolling operation transfers
  157. region A downward so part of it is copied into area B.  The area C (which is
  158. the same size as area B) at the top of the scrolling region is filled with
  159. pixels of the current color index (as defined in the most recent call to
  160. fg_setcolor), and the original contents of area B are lost.  The circular
  161. scrolling example also copies region A downward into the original area B.
  162. Unlike end-off scrolling, however, circular scrolling preserves the area B by
  163. copying it to the opposite edge of the scrolling region.
  164.  
  165.      The fg_scroll routine takes six arguments.  The first four define the
  166. scrolling region in the order minimum x coordinate, maximum x coordinate,
  167. minimum y coordinate, and maximum y coordinate.  In graphics video modes, the
  168. x coordinates are extended by byte boundaries if needed.  The fifth argument
  169. is the scrolling increment.  It specifies the number of rows to scroll.  If
  170. it is positive, the scrolling direction is toward the bottom of the screen;
  171. if it is negative, the scrolling direction is toward the top of the screen.
  172. The sixth and final argument specifies the scroll type.  If this value is
  173. zero, the scroll will be circular; if it is any other value, the scroll will
  174. be end-off.  If the scroll type is circular, Fastgraph will use the hidden
  175. page (as defined in the most recent call to fg_sethpage) as a workspace (more
  176. specifically, the area bounded by the scrolling region extremes on the hidden
  177. page will be used).  We'll now present three example programs that use the
  178. fg_scroll routine.
  179.  
  180.      Example 13-3 runs in any 320 by 200 graphics video mode.  The program
  181. displays two lines of text ("line one" and "line two") in the upper left
  182. corner of the screen against a white background.  It then uses the fg_scroll
  183. routine to move the second line down four pixel rows using an end-off scroll.
  184.                                             Chapter 13:  Special Effects   253
  185.  
  186. After waiting for a keystroke, the program again uses fg_scroll to move the
  187. text back to its original position.  Note especially how the fg_setcolor
  188. routine appears before the first call to fg_scroll to replace the "scrolled
  189. off" rows with pixels of color 15, thus preserving the white background.
  190.  
  191.                                 Example 13-3.
  192.  
  193.                 #include <fastgraf.h>
  194.                 #include <stdio.h>
  195.                 #include <stdlib.h>
  196.                 void main(void);
  197.  
  198.                 void main()
  199.                 {
  200.                    int new_mode, old_mode;
  201.  
  202.                    new_mode = fg_bestmode(320,200,1);
  203.                    if (new_mode < 0 || new_mode == 12) {
  204.                       printf("This program requires a 320 ");
  205.                       printf("x 200 color graphics mode.\n");
  206.                       exit(1);
  207.                       }
  208.                    old_mode = fg_getmode();
  209.                    fg_setmode(new_mode);
  210.  
  211.                    fg_setcolor(15);
  212.                    fg_rect(0,319,0,199);
  213.                    fg_setcolor(10);
  214.                    fg_text("line one",8);
  215.                    fg_locate(1,0);
  216.                    fg_text("line two",8);
  217.                    fg_waitkey();
  218.  
  219.                    fg_setcolor(15);
  220.                    fg_scroll(0,63,8,15,4,1);
  221.                    fg_waitkey();
  222.                    fg_scroll(0,63,12,19,-4,1);
  223.                    fg_waitkey();
  224.  
  225.                    fg_setmode(old_mode);
  226.                    fg_reset();
  227.                 }
  228.  
  229.      Example 13-4 is similar to example 13-3, but it runs in the 80-column
  230. color text mode (mode 3).  In text modes, we cannot scroll half a character
  231. row (four pixels) as in example 13-3, so the program scrolls the minimum one
  232. row instead.
  233.  
  234.                                 Example 13-4.
  235.  
  236.                          #include <fastgraf.h>
  237.                          void main(void);
  238.  
  239.                          void main()
  240.                          {
  241.                             int old_mode;
  242.  
  243. 254   Fastgraph User's Guide
  244.  
  245.  
  246.                             old_mode = fg_getmode();
  247.                             fg_setmode(3);
  248.                             fg_cursor(0);
  249.  
  250.                             fg_setcolor(7);
  251.                             fg_rect(0,79,0,24);
  252.                             fg_setattr(10,7,0);
  253.                             fg_text("line one",8);
  254.                             fg_locate(1,0);
  255.                             fg_text("line two",8);
  256.                             fg_waitkey();
  257.  
  258.                             fg_setcolor(7);
  259.                             fg_scroll(0,7,1,1,1,1);
  260.                             fg_waitkey();
  261.                             fg_scroll(0,7,2,2,-1,1);
  262.                             fg_waitkey();
  263.  
  264.                             fg_setmode(old_mode);
  265.                             fg_reset();
  266.                          }
  267.  
  268.      Example 13-5, the final scrolling example, demonstrates a circular
  269. scroll.  The program runs in any 320 by 200 color graphics video mode; note
  270. the use of video page 1 for the workspace required when the fg_scroll routine
  271. performs a circular scroll.  The program first fills the screen with a light
  272. blue rectangle (cyan in CGA), displays a smaller white rectangle in the
  273. center of the screen, and then uses fg_move, fg_draw, and fg_paint to display
  274. a light green star (magenta in CGA) within the white rectangle.  The program
  275. executes a while loop to scroll the star upward in four pixel increments.
  276. Because the scroll is circular, rows of the star that "scroll off" the top
  277. edge of the white rectangle (whose height is the same as the scrolling
  278. region) reappear at its bottom edge.  The use of fg_waitfor within the loop
  279. simply slows down the scroll.  The scrolling continues until any key is
  280. pressed.
  281.  
  282.                                 Example 13-5.
  283.  
  284.                 #include <conio.h>
  285.                 #include <fastgraf.h>
  286.                 #include <stdio.h>
  287.                 #include <stdlib.h>
  288.                 void main(void);
  289.  
  290.                 void main()
  291.                 {
  292.                    int new_mode, old_mode;
  293.  
  294.                    new_mode = fg_bestmode(320,200,2);
  295.                    if (new_mode < 0 || new_mode == 12) {
  296.                       printf("This program requires a 320 ");
  297.                       printf("x 200 color graphics mode.\n");
  298.                       exit(1);
  299.                       }
  300.                    old_mode = fg_getmode();
  301.  
  302.                                             Chapter 13:  Special Effects   255
  303.  
  304.  
  305.                    fg_setmode(new_mode);
  306.                    fg_allocate(1);
  307.                    fg_sethpage(1);
  308.  
  309.                    fg_setcolor(9);
  310.                    fg_rect(0,319,0,199);
  311.                    fg_setcolor(15);
  312.                    fg_rect(132,188,50,150);
  313.  
  314.                    fg_setcolor(10);
  315.                    fg_move(160,67);
  316.                    fg_draw(175,107);
  317.                    fg_draw(140,82);
  318.                    fg_draw(180,82);
  319.                    fg_draw(145,107);
  320.                    fg_draw(160,67);
  321.                    fg_paint(160,77);
  322.                    fg_paint(150,87);
  323.                    fg_paint(160,87);
  324.                    fg_paint(170,87);
  325.                    fg_paint(155,97);
  326.                    fg_paint(165,97);
  327.  
  328.                    while (kbhit() == 0) {
  329.                       fg_waitfor(1);
  330.                       fg_scroll(136,184,50,150,-4,0);
  331.                       }
  332.                    fg_waitkey();
  333.  
  334.                    fg_freepage(1);
  335.                    fg_setmode(old_mode);
  336.                    fg_reset();
  337.                 }
  338.  
  339.  
  340. Changing the Screen Origin
  341.  
  342.      Fastgraph includes two routines for changing the screen origin.  By
  343. changing the screen origin, we simply mean defining the (x,y) coordinate of
  344. the upper left corner of the display area.  The fg_pan routine performs this
  345. function in screen space, while the fg_panw routine does in world space.
  346. Neither routine changes the graphics cursor position.
  347.  
  348.      Each of these routines has two arguments that specify the x and y
  349. coordinates of the screen origin.  For the fg_pan routine, the arguments are
  350. integer quantities.  For the fg_panw routine, they are floating point
  351. quantities.
  352.  
  353.      In the EGA, VGA, MCGA, XVGA, and SVGA graphics modes (modes 13 to 29),
  354. you can set the screen origin to any (x,y) coordinate position (that is, to
  355. any pixel).  In the other graphics modes, certain restrictions exist, as
  356. imposed by specific video hardware.  These constraints limit the coordinate
  357. positions that can be used as the screen origin.  Fastgraph compensates for
  358. these restrictions by reducing the specified x and y coordinates to values
  359. that are acceptable to the current video mode, as shown in the following
  360. table.
  361. 256   Fastgraph User's Guide
  362.  
  363.  
  364.                            x will be reduced   y will be reduced
  365.           video mode       to a multiple of:   to a multiple of:
  366.  
  367.               4-5                  8                   2
  368.                6                  16                   2
  369.                9                   4                   4
  370.               11                   8                   4
  371.               12                   4                2 or 3
  372.  
  373. In modes 4 and 5, for instance, the x coordinate will be reduced to a
  374. multiple of 8 pixels, and the y coordinate will be reduced to a multiple of 2
  375. pixels.  In the Hercules low resolution mode (mode 12), the y coordinate
  376. reduction depends on whether or not the specified pixel row is scan doubled.
  377.  
  378.      Example 13-6 shows a useful effect that can be made with the fg_pan or
  379. fg_panw routines.  This program uses the fg_automode routine to select a
  380. video mode and then draws an unfilled white rectangle.  The top and bottom
  381. sides of the rectangle are intentionally drawn just smaller than the physical
  382. screen size.  After waiting for a keystroke, the program uses a for loop to
  383. make the rectangle jiggle up and down.  The rectangle moves because the
  384. fg_pan routine is called inside the loop to switch the screen origin between
  385. the rectangle's upper left corner and the original origin.  Note also the use
  386. of the fg_waitfor routine to cause slight delays after each call to fg_pan.
  387. If we didn't use fg_waitfor, the changing of the origin would occur so
  388. rapidly we wouldn't notice the effect.  Finally, the program restores the
  389. original video mode and screen attributes before returning to DOS.
  390.  
  391.                                 Example 13-6.
  392.  
  393.                  #include <fastgraf.h>
  394.                  #include <stdio.h>
  395.                  #include <stdlib.h>
  396.                  void main(void);
  397.  
  398.                  #define DELAY 2
  399.                  #define JUMP  4
  400.  
  401.                  void main()
  402.                  {
  403.                     int i;
  404.                     int old_mode;
  405.  
  406.                     old_mode = fg_getmode();
  407.                     fg_setmode(fg_automode());
  408.  
  409.                     fg_setcolor(15);
  410.                     fg_move(0,JUMP);
  411.                     fg_draw(fg_getmaxx(),JUMP);
  412.                     fg_draw(fg_getmaxx(),fg_getmaxy()-JUMP);
  413.                     fg_draw(0,fg_getmaxy()-JUMP);
  414.                     fg_draw(0,JUMP);
  415.                     fg_waitkey();
  416.  
  417.                     for (i = 0; i < 6; i++) {
  418.                        fg_pan(0,JUMP);
  419.                        fg_waitfor(DELAY);
  420.                                             Chapter 13:  Special Effects   257
  421.  
  422.  
  423.                        fg_pan(0,0);
  424.                        fg_waitfor(DELAY);
  425.                        }
  426.  
  427.                     fg_setmode(old_mode);
  428.                     fg_reset();
  429.                  }
  430.  
  431.  
  432.      The real power of the fg_pan routine becomes clear when it is used with
  433. the fg_resize routine to perform smooth panning.  Recall from Chapter 8 that
  434. fg_resize changes the video page dimensions in native EGA and VGA graphics
  435. modes (modes 13 to 18), the extended VGA graphics modes (20 to 23), and the
  436. SVGA graphics modes (24 to 29).  We'll now present an example that shows how
  437. to use these two routines to perform panning in the low-resolution EGA
  438. graphics mode (mode 13).  The method it uses also would work in any mode that
  439. supports video page resizing.
  440.  
  441.      Example 13-7 begins by establishing the video mode and then immediately
  442. calls fg_resize to increase the video page size to 640 by 400 pixels.  Thus,
  443. the video page is now four times its original size.  Following this, the
  444. program fills the page (the entire page, not just what is displayed) with a
  445. bright green rectangle with a white border around it.  It then displays the
  446. message "Press arrow keys to pan" as close as possible to the center of the
  447. page.
  448.  
  449.      The main part of the program is a loop that accepts keystrokes and then
  450. calls fg_pan to perform the panning one pixel at a time.  When you press any
  451. of the four arrow keys, the program adjusts the x and y coordinates for the
  452. screen origin as directed.  For example, pressing the up arrow key scrolls
  453. the screen upward one pixel.  Note that when we reach the edge of the video
  454. page, the program prevents further scrolling in that direction.  This process
  455. continues until you press the Esc key, at which time the program restores the
  456. original video mode and screen attributes before exiting.
  457.  
  458.                                 Example 13-7.
  459.  
  460.                   #include <fastgraf.h>
  461.                   void main(void);
  462.  
  463.                   void main()
  464.                   {
  465.                      unsigned char key, aux;
  466.                      int old_mode;
  467.                      int x, y;
  468.  
  469.                      old_mode = fg_getmode();
  470.                      fg_setmode(13);
  471.                      fg_resize(640,400);
  472.  
  473.                      fg_setcolor(2);
  474.                      fg_rect(0,fg_getmaxx(),0,fg_getmaxy());
  475.                      fg_setcolor(15);
  476.                      fg_box(0,fg_getmaxx(),0,fg_getmaxy());
  477.                      fg_locate(24,28);
  478.                      fg_text("Press arrow keys to pan.",24);
  479. 258   Fastgraph User's Guide
  480.  
  481.  
  482.                      x = 0;
  483.                      y = 0;
  484.  
  485.                      do {
  486.                         fg_getkey(&key,&aux);
  487.                         if (aux == 72 && y < 200)
  488.                            y++;
  489.                         else if (aux == 75 && x < 320)
  490.                            x++;
  491.                         else if (aux == 77 && x > 0)
  492.                            x--;
  493.                         else if (aux == 80 && y > 0)
  494.                            y--;
  495.                         fg_pan(x,y);
  496.                      } while (key != 27);
  497.  
  498.                      fg_setmode(old_mode);
  499.                      fg_reset();
  500.                   }
  501.  
  502.  
  503.  
  504. Summary of Special Effects Routines
  505.  
  506.      This section summarizes the functional descriptions of the Fastgraph
  507. routines presented in this chapter.  More detailed information about these
  508. routines, including their arguments and return values, may be found in the
  509. Fastgraph Reference Manual.
  510.  
  511.      FG_FADEIN incrementally replaces the visual page contents with the
  512. hidden page contents.  This routine has no effect in text video modes.
  513.  
  514.      FG_FADEOUT incrementally replaces the visual page contents with pixels
  515. of the current color.  This routine has no effect in text video modes.
  516.  
  517.      FG_PAN changes the screen origin (the upper left corner of the screen)
  518. to the specified screen space coordinates.  This routine has no effect in
  519. text video modes.
  520.  
  521.      FG_PANW is the world space version of the fg_pan routine.
  522.  
  523.      FG_RESIZE changes the dimensions of a video page in EGA and VGA graphics
  524. modes.
  525.  
  526.      FG_SCROLL vertically scrolls a region of the active video page.  The
  527. scrolling may be done either up or down, using either an end-off or circular
  528. method.  Circular scrolling uses part of the hidden page as a temporary
  529. workspace.
  530.